//
//  MainViewController.m
//  weibo110
//
//  Created by 臧松 on 15/12/25.
//  Copyright © 2015年 臧松. All rights reserved.
//

#import "MainViewController.h"
#import "HomeTableViewController.h"
#import "MessageTableViewController.h"
#import "DiscoverTableViewController.h"
#import "ProfileTableViewController.h"
#import "UIButton+Extension.h"
@interface MainViewController ()
// 添加按钮
@property (nonatomic,strong)UIButton* btn;

@end
@implementation MainViewController
-(UIButton *)btn{
    if (_btn == nil) {
        _btn =[UIButton btnWithImageName:@"tabbar_compose_icon_add" backImageName:@"tabbar_compose_button"];
    }
    return _btn;
}
// 视图的生命周期函数
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tabBar bringSubviewToFront:self.btn];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self addChildViewControllers];
    [self setupComposeButton];
}
// 设置撰写按钮
-(void)setupComposeButton{
    // 添加按钮
    [self.tabBar addSubview:self.btn];
    // 调整按钮位置
    NSInteger count = self.childViewControllers.count;
    // 让按钮框一点 解决手指触摸容错处理
    CGFloat w = self.tabBar.bounds.size.width/ (CGFloat)count - 1;
    self.btn.frame = CGRectInset(self.tabBar.bounds, 2*w, 0);
    // NSLog(@"%@",NSStringFromCGRect(self.btn.frame));
    
    // 添加按钮点击事件
    [self.btn addTarget:self action:@selector(clickComposeButton) forControlEvents:UIControlEventTouchUpInside];
    
}
// 按钮点击事件
-(void)clickComposeButton{
    NSLog(@"点我了");
}
// MARK: - 设置界面
-(void)addChildViewControllers{
   
    // 设置 tabBar 的颜色
    self.tabBar.tintColor = [UIColor orangeColor];

    [self addChildViewController:[[HomeTableViewController alloc]init] :@"首页" :@"tabbar_home"];
    [self addChildViewController:[[MessageTableViewController alloc]init] :@"消息" :@"tabbar_message_center"];
    [self addChildViewController:[[UIViewController alloc]init]];
    [self addChildViewController:[[DiscoverTableViewController alloc]init] :@"发现" :@"tabbar_discover"];
    [self addChildViewController:[[ProfileTableViewController alloc]init] :@"我" :@"tabbar_profile"];
}
// 添加控制器
-(void)addChildViewController:(UIViewController *)vc :(NSString*)title :(NSString*)imageName{
    
    // 设置标题 - 由内而外设置
    vc.title = title;
    // 设置图像
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 导航控制器
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
    //nav.tabBarItem.title = @"首页";
    [self addChildViewController:nav];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
